import sys
from PyQt5.QtCore import Qt, QPoint, QRect
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QLabel, QPushButton


class FramelessWindow(QWidget):
    def __init__(self):
        super().__init__()
        self.initUI()

        # 设置初始状态
        self.is_resizing = False
        self.is_moving = False
        self.resize_margin = 10  # 缩放边距
        self.start_position = None
        self.current_resize_direction = None

    def initUI(self):
        self.setWindowFlags(Qt.FramelessWindowHint)  # 无边框窗口
        self.setAttribute(Qt.WA_TranslucentBackground)  # 支持透明背景
        self.setStyleSheet("background-color: white; border-radius: 10px;")  # 窗口样式
        self.setGeometry(200, 200, 800, 600)

        # 添加一个简单的内容布局
        layout = QVBoxLayout(self)

        # 顶部标题
        title_label = QLabel("无边框窗口", self)
        title_label.setAlignment(Qt.AlignCenter)
        title_label.setStyleSheet("font-size: 20px; font-weight: bold;")
        layout.addWidget(title_label)

        # 关闭按钮
        close_button = QPushButton("关闭", self)
        close_button.clicked.connect(self.close)
        close_button.setStyleSheet("background-color: red; color: white; border-radius: 5px;")
        layout.addWidget(close_button, alignment=Qt.AlignRight)

    def mousePressEvent(self, event):
        """鼠标按下事件"""
        if event.button() == Qt.LeftButton:
            self.start_position = event.globalPos()
            self.current_geometry = self.geometry()
            self.is_resizing = self.is_in_resize_area(event.pos())  # 判断是否为缩放区域
            self.is_moving = not self.is_resizing

    def mouseMoveEvent(self, event):
        """鼠标移动事件"""
        if self.is_moving:
            # 拖动窗口
            delta = event.globalPos() - self.start_position
            self.move(self.pos() + delta)
            self.start_position = event.globalPos()
        elif self.is_resizing:
            # 调整窗口大小
            self.resize_window(event.globalPos())
        else:
            # 更新鼠标样式
            self.setCursorByPosition(event.pos())

    def mouseReleaseEvent(self, event):
        """鼠标释放事件"""
        self.is_moving = False
        self.is_resizing = False

    def is_in_resize_area(self, pos):
        """判断鼠标是否在边框的缩放区域内"""
        rect = self.rect()
        margin = self.resize_margin
        self.current_resize_direction = None

        # 判断鼠标在哪个区域
        if pos.x() >= rect.width() - margin and pos.y() >= rect.height() - margin:
            self.current_resize_direction = "bottom-right"  # 右下角
        elif pos.x() <= margin and pos.y() >= rect.height() - margin:
            self.current_resize_direction = "bottom-left"  # 左下角
        elif pos.x() >= rect.width() - margin:
            self.current_resize_direction = "right"  # 右边
        elif pos.y() >= rect.height() - margin:
            self.current_resize_direction = "bottom"  # 下边
        elif pos.x() <= margin:
            self.current_resize_direction = "left"  # 左边
        elif pos.y() <= margin:
            self.current_resize_direction = "top"  # 上边
        return self.current_resize_direction is not None

    def resize_window(self, global_pos):
        """根据鼠标位置调整窗口大小"""
        if self.current_resize_direction is None:
            return

        delta = global_pos - self.start_position
        rect = self.current_geometry

        if self.current_resize_direction == "bottom-right":
            rect.setWidth(max(self.minimumWidth(), rect.width() + delta.x()))
            rect.setHeight(max(self.minimumHeight(), rect.height() + delta.y()))
        elif self.current_resize_direction == "bottom":
            rect.setHeight(max(self.minimumHeight(), rect.height() + delta.y()))
        elif self.current_resize_direction == "right":
            rect.setWidth(max(self.minimumWidth(), rect.width() + delta.x()))
        elif self.current_resize_direction == "bottom-left":
            rect.setLeft(rect.left() + delta.x())
            rect.setHeight(max(self.minimumHeight(), rect.height() + delta.y()))
        elif self.current_resize_direction == "left":
            rect.setLeft(rect.left() + delta.x())
        elif self.current_resize_direction == "top":
            rect.setTop(rect.top() + delta.y())

        self.setGeometry(rect)

    def setCursorByPosition(self, pos):
        """根据鼠标位置设置光标样式"""
        rect = self.rect()
        margin = self.resize_margin

        # 判断鼠标在哪个区域
        if pos.x() >= rect.width() - margin and pos.y() >= rect.height() - margin:
            self.setCursor(Qt.SizeFDiagCursor)  # 右下角
        elif pos.x() <= margin and pos.y() >= rect.height() - margin:
            self.setCursor(Qt.SizeBDiagCursor)  # 左下角
        elif pos.x() >= rect.width() - margin:
            self.setCursor(Qt.SizeHorCursor)  # 右边
        elif pos.y() >= rect.height() - margin:
            self.setCursor(Qt.SizeVerCursor)  # 下边
        elif pos.x() <= margin:
            self.setCursor(Qt.SizeHorCursor)  # 左边
        elif pos.y() <= margin:
            self.setCursor(Qt.SizeVerCursor)  # 上边
        else:
            self.setCursor(Qt.ArrowCursor)  # 默认样式

    def event(self, event):
        """处理通用事件（更新光标）"""
        if event.type() == 10:  # QEvent.HoverMove
            self.setCursorByPosition(event.pos())
        return super().event(event)


if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = FramelessWindow()
    window.show()
    sys.exit(app.exec_())
